

#include "unity.h"
#include "unity_fixture.h"

#include "log/log2.h"
#include "BSP_Sleep.h"

#include "FreeRTOS.h"
#include "task.h"

#include <phApp_Init.h>
#include <Reader/Reader.h>

phStatus_t phApp_Configure_Power(void);

TEST_GROUP(PN5190);


TEST_SETUP(PN5190)
{
	reader.hfFlags = HFTAG_SUPPORT;
	Reader_NFC_LoadProfile(reader.hfFlags, 0, 0, 0);
}

TEST_TEAR_DOWN(PN5190)
{
	Reader_NFC_RfOff();
}

static uint32_t pn5190_poll(uint32_t flags, uint8_t count){
	SimpleTag_T simpleTag;
	int ret;
	uint32_t t1,t2;
	t1 = xTaskGetTickCount();
	ret = Reader_NFC_DiscoveryStd3(&simpleTag, flags, count);
	t2 = xTaskGetTickCount();
	return t2 - t1;
}

// TEST(PN5190, polling)
// {
// 	SimpleTag_T simpleTag;
// 	int ret;
// 	uint32_t t1,t2;
// 	while(1){
// 		Reader_NFC_RfOff();
// 		BSP_Sleep_Ms(1000);
// //			t1 = xTaskGetTickCount();
// //			ret = Reader_NFC_DiscoveryStd3(&simpleTag, PHAC_DISCLOOP_POS_BIT_MASK_F212|PHAC_DISCLOOP_POS_BIT_MASK_F424, 1);
// //			t2 = xTaskGetTickCount();
// //			phApp_Configure_Power();
// //			LOG2_PRINT("discover Felica time: %u\n", (t2-t1));
// 		log_reset_level();
// 		uint32_t t = pn5190_poll(PHAC_DISCLOOP_POS_BIT_MASK_F212|PHAC_DISCLOOP_POS_BIT_MASK_F424, 2);
// 		LOG2_INF("discover Felica time: %u\n", t);
// 		t = pn5190_poll(PHAC_DISCLOOP_POS_BIT_MASK_A, 2);
// 		LOG2_INF("discover A time: %u\n", t);
// 		t = pn5190_poll(PHAC_DISCLOOP_POS_BIT_MASK_B, 2);
// 		LOG2_INF("discover B time: %u\n", t);
// 		t = pn5190_poll(PHAC_DISCLOOP_POS_BIT_MASK_V, 2);
// 		LOG2_INF("discover V time: %u\n", t);
// 		uint32_t flags = PHAC_DISCLOOP_POS_BIT_MASK_F212|PHAC_DISCLOOP_POS_BIT_MASK_F424
// 			|PHAC_DISCLOOP_POS_BIT_MASK_A
// 			|PHAC_DISCLOOP_POS_BIT_MASK_B
// 			|PHAC_DISCLOOP_POS_BIT_MASK_V;
// 		t = pn5190_poll(flags, 1);
// 		LOG2_INF("discover ALL time: %u\n", t);
// 	}
// }

phStatus_t read_hid_iclass_card(uint8_t *uid, uint8_t *len);

TEST(PN5190, iclass)
{
	SimpleTag_T simpleTag;
	int ret;
	uint32_t t1,t2;
	uint8_t uid[10];
	uint8_t len = 10;
	while(1){
		Reader_NFC_RfOff();
		BSP_Sleep_Ms(1000);

		log_reset_level();
		phStatus_t status = read_hid_iclass_card(uid, &len);
		if(status == PH_ERR_SUCCESS){
			LOG2_BUF_INF("iclass card uid: %s, len: %d\n", uid, len);
		}
	}
}
